package org.jeecg.modules.util;

import java.util.*;
import java.util.stream.Collectors;

class Reviewer {
    private String id;
    private String province;

    public Reviewer(String id, String province) {
        this.id = id;
        this.province = province;
    }

    public String getId() { return id; }
    public String getProvince() { return province; }
}

class Case {
    private String province;

    public Case(String province) {
        this.province = province;
    }

    public String getProvince() { return province; }
}

public class CaseAssignment {

    public static void main(String[] args) {
        List<Case> cases = Arrays.asList(
                new Case("A"), new Case("B"), new Case("C"), new Case("A")
        );
        List<Reviewer> reviewers = Arrays.asList(
                new Reviewer("R1", "B"),
                new Reviewer("R2", "C"),
                new Reviewer("R3", "D"),
                new Reviewer("R4", "E"),
                new Reviewer("R5", "F"),
                new Reviewer("R6", "B"),
                new Reviewer("R7", "C"),
                new Reviewer("R8", "D"),
                new Reviewer("R9", "E"),
                new Reviewer("R10", "F")
        );

        Map<Reviewer, Integer> assignmentCount = new HashMap<>();
        reviewers.forEach(reviewer -> assignmentCount.put(reviewer, 0));

        Map<Case, List<Reviewer>> assignmentResult = new HashMap<>();

        for (Case currentCase : cases) {
            String caseProvince = currentCase.getProvince();
            List<Reviewer> candidates = reviewers.stream()
                    .filter(r -> !r.getProvince().equals(caseProvince))
                    .collect(Collectors.toList());

            if (candidates.size() < 3) {
                System.err.println("案例省份 " + caseProvince + " 无法分配足够评审。");
                continue;
            }

            // 按分配次数分组并随机排序同次数的评审
            Map<Integer, List<Reviewer>> reviewersByCount = new HashMap<>();
            candidates.forEach(r -> {
                int count = assignmentCount.get(r);
                reviewersByCount.computeIfAbsent(count, k -> new ArrayList<>()).add(r);
            });

            List<Integer> sortedCounts = reviewersByCount.keySet().stream()
                    .sorted()
                    .collect(Collectors.toList());

            List<Reviewer> shuffledCandidates = new ArrayList<>();
            sortedCounts.forEach(count -> {
                List<Reviewer> group = new ArrayList<>(reviewersByCount.get(count));
                Collections.shuffle(group); // 随机打乱同次数的组
                shuffledCandidates.addAll(group);
            });

            // 选择前三个未被分配到当前案例的评审
            List<Reviewer> selected = new ArrayList<>();
            for (Reviewer reviewer : shuffledCandidates) {
                if (selected.size() >= 3) {
                    break;
                }
                // 检查该评审是否已被分配到当前案例（防止重复分配）
                if (!selected.contains(reviewer)) {
                    selected.add(reviewer);
                }
            }

            if (selected.size() < 3) {
                System.err.println("案例省份 " + caseProvince + " 无法分配足够评审。");
                continue;
            }

            assignmentResult.put(currentCase, new ArrayList<>(selected));

            selected.forEach(r -> assignmentCount.put(r, assignmentCount.get(r) + 1));
        }

        // 输出分配结果
        assignmentResult.forEach((c, rs) -> {
            System.out.println("案例省份 " + c.getProvince() + " 分配评审: " +
                    rs.stream().map(Reviewer::getId).collect(Collectors.joining(", ")));
        });

        // 输出各评审分配次数
        System.out.println("\n各评审分配次数:");
        assignmentCount.forEach((r, count) ->
                System.out.println(r.getId() + ": " + count + " 次")
        );
    }
}